package com.yjt.fresh.adpater;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yjt.fresh.R;
import com.yjt.fresh.bean.BeanCollection;
import com.yjt.fresh.bean.BeanCoupon;
import com.yjt.fresh.service.Utils;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.List;

/**
 * 优惠券中心适配器
 * Created by mcc on 2018/6/10.
 */

public class AdpRvAtyMyCollection extends BaseQuickAdapter<BeanCollection, BaseViewHolder> {
    public AdpRvAtyMyCollection(int layoutResId, @Nullable List<BeanCollection> data) {
        super(layoutResId, data);
    }

    private boolean mIsShowCb;
    private boolean mIsCheck;

    public void isCheck(boolean isCheck){
        mIsCheck = isCheck;
        notifyDataSetChanged();
    }


    public void isShowCb(boolean isShowCb){
        mIsShowCb = isShowCb;
        notifyDataSetChanged();
    }
    @Override
    protected void convert(BaseViewHolder helper, final BeanCollection item) {
        CheckBox cb = helper.getView(R.id.cbAppAtyMyCollection);
        ImageView iv = helper.getView(R.id.ivAppMyCollection);
        TextView tvTitle = helper.getView(R.id.tvAppAtyMyCollectionTitle);
        TextView tvNewPrice = helper.getView(R.id.tvAppAtyMyCollectionNewPrice);
        TextView tvOldPrice = helper.getView(R.id.tvAppAtyMyCollectionOldPrice);
        TextView tvSaleCount = helper.getView(R.id.tvAppAtyMyCollectionSaleCount);
        TextView tvDistance = helper.getView(R.id.tvAppAtyMyCollectionDistance);
        TextView tvDelete = helper.getView(R.id.tvAppRecycleItemAtyMyCollectionDelete);

        LinearLayout llContent = helper.getView(R.id.llAppRecycleItemAtyMyCollectionContent);

        Glide.with(mContext).load(item.getGOODS_BREVIARY_IMG()).into(iv);
        tvTitle.setText(item.getGOODS_TITLE());
        tvNewPrice.setText(item.getGOODS_DISPLAY_PRICE());
        tvSaleCount.setText("已售   "+(item.getREAL_GROUP_COUNT() == null ? "0" : item.getREAL_GROUP_COUNT()));

        String[] location = item.getLATITUDE_LONGITUDE().split(",");
        String latitude = PreferenceManager.getInstance().getLatitude();
        String longitude = PreferenceManager.getInstance().getLongitude();
        double distance = Utils.getDistance(Double.valueOf(longitude), Double.valueOf(latitude), Double.valueOf(location[0]), Double.valueOf(location[1]));
        tvDistance.setText(distance +"km");

        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyCollectionDelete);

        helper.addOnClickListener(R.id.llAppRecycleItemAtyMyCollectionContent);
        cb.setChecked(mIsCheck);

        if (mIsShowCb){
            cb.setVisibility(View.VISIBLE);
        }else {
            cb.setVisibility(View.GONE);
        }

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setCheck(isChecked);
            }
        });
    }
}
